<template>
  <div style="margin:10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="支付类型" prop="type">
        <el-select style="width: 145px" v-model="queryParams.type"
                   placeholder="请选择支付类型"
                   clearable>
          <el-option
            label="查询"
            value="query">
          </el-option>
          <el-option
            label="升级"
            value="upgrade">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户账号" prop="userName">
        <el-input style="width:145px" v-model="queryParams.userName"
                  placeholder="请输入用户账号"
                  clearable/>
      </el-form-item>
      <el-form-item label="关联单号" prop="outId">
        <el-input style="width:145px" v-model="queryParams.outId"
                  placeholder="请输入关联单号"
                  clearable/>
      </el-form-item>
      <el-form-item label="系统单号" prop="orderNo">
        <el-input style="width:145px" v-model="queryParams.orderNo"
                  placeholder="请输入系统单号"
                  clearable/>
      </el-form-item>
      <el-form-item label="查询项目" prop="productId">
        <el-select style="width:145px" v-model="queryParams.productId"
                   placeholder="请选择" clearable>
          <el-option
            v-for="item in productOptions"
            :label="item.text"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="平台单号" prop="platformOrderNo">
        <el-input style="width:145px" v-model="queryParams.platformOrderNo"
                  placeholder="请输入平台单号"
                  clearable/>
      </el-form-item>
      <el-form-item label="支付状态" prop="payState">
        <el-select style="width:145px" v-model="queryParams.payState"
                   placeholder="请选择支付状态" clearable>
          <el-option
            v-for="item in payState"
            :label="item.text"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-delete" size="mini" type="danger" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['system:pay:remove']">删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="payList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="用户账号" align="center" prop="userName"/>
      <el-table-column label="系统单号" align="center" prop="orderNo"/>
      <el-table-column label="类型" align="center" prop="type"/>
      <el-table-column label="关联单号" align="center" prop="outId"/>
      <el-table-column label="商品标题" align="center" prop="subject"/>
      <el-table-column label="支付金额" align="center" prop="pricing"/>
      <el-table-column label="平台单号" align="center" prop="platformOrderNo"/>
      <el-table-column label="支付方式" align="center" prop="payType"/>
      <el-table-column label="支付状态" align="center" prop="payState"/>
      <el-table-column label="付款时间" align="center" prop="payTime" width="120"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="120"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <button @click="handleDelete(scope.row)">删除</button>
          <button @click="refund_(scope.row)" v-if="scope.row.payState=='SUCCESS'">退款</button>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
  </div>
</template>

<script>
  import {distinctName} from "@/api/system/product";
  import {listPay, delPay, refund} from "@/api/system/pay";

  export default {
    name: "Pay",
    data() {
      return {
        payState: [{
          value: 'SUCCESS',
          text: '成功'
        }, {
          value: 'REFUND',
          text: '退款'
        }, {
          value: 'NOTPAY',
          text: '未支付'
        }, {
          value: 'CLOSED',
          text: '关闭'
        }, {
          value: 'USERPAYING',
          text: '支付中'
        }, {
          value: 'PAYERROR',
          text: '支付异常'
        }
        ],
        productOptions: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 支付表格数据
        payList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,

          type: null,
          userName: null,
          outId: null,
          orderNo: null,
          productId: null,
          platformOrderNo: null,
          payState: null
        }
      };
    },
    created() {
      this.getList()
      distinctName().then(res => {
        this.productOptions = res.product
      })
    },
    methods: {
      refund_(row) {
        this.$prompt('此订单支付金额：' + row.pricing, '请输入退款金额', {
          closeOnClickModal: false,
          closeOnPressEscape: false,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputType: 'number',
          inputValidator: (value) => {       // 点击按钮时，对文本框里面的值进行验证
            if (value <= 0) {
              return '退款金额要大于0'
            }
            if (value > row.pricing) {
              return '退款金额要小于支付额'
            }
          }
        }).then(({value}) => {
          let this_ = this
          refund({
            id: row.id,
            amount: value
          }).then(response => {
            if (response.code == 200) {
              this_.$modal.msgSuccess("退款成功")
            }
            this_.getList();
          });
        }).catch(() => {
          this.loading = false
        })
      },
      /** 查询支付列表 */
      getList() {
        this.loading = true;
        listPay(this.queryParams).then(response => {
          this.payList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除支付编号为"' + ids + '"的数据项？').then(function () {
          return delPay(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      }
    }
  };
</script>
